//
//  PictureCollectionView.swift
//  DSWeiBo
//
//  Created by agg-hf-macmini-2 on 16/8/3.
//  Copyright © 2016年 LiuXing. All rights reserved.
//

import UIKit

class PictureCollectionView: UICollectionView {
    // MARK:- 定义属性
    var picURLs : [NSURL] = [NSURL]() {
        didSet {
            self.reloadData()
        }
    }
    override func awakeFromNib() {
         super.awakeFromNib()
        dataSource = self
    }
}

extension PictureCollectionView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picURLs.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 1.获取cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("picCell", forIndexPath: indexPath) as! PictureCollectionCell
        
        // 2.给cell设置数据
        cell.picURL = picURLs[indexPath.item]
        
        return cell
    }
}

class PictureCollectionCell: UICollectionViewCell {
    // MARK:- 定义模型属性
    var picURL : NSURL? {
        didSet {
            guard let picURL = picURL else {
                return
            }
            
            iconView.sd_setImageWithURL(picURL, placeholderImage: UIImage(named: "empty_picture"))
        }
    }
    @IBOutlet weak var iconView: UIImageView!
}